<?php

namespace App\Exports\Flight;

use App\Services\Flight\FlightSearchService;
use Maatwebsite\Excel\Concerns\FromCollection;

class FlightDynamicDelayExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        //

        $data[] = [
            '日期',
            '航班号',
            '起飞',
            '到达',
            '历史出发准点率',
            '历史到达准点率',
            '出发延误30分钟内百分比',
            '出发延误30分钟-1小时内百分比',
            '出发延误1小时-3小时内百分比',
            '出发延误3小时以上百分比',
            '到达延误30分钟内百分比',
            '到达延误30分钟-1小时内百分比',
            '到达延误1小时-3小时内百分比',
            '到达延误3小时以上百分比',
            '出发平均延误时间',
            '到达平均延误时间',
            '航班取消率',
            '七日准点情况',
            '调时率',
            '近期调时航班，格式为“日期，航班是否取消，航班是否调时，调时时长（计划起飞时间延误时长，复数代表提前，单位：分钟）',
            '合计'
        ];

        $flightDynamicService = new FlightSearchService();
        $interface = resolve('App\Repositories\Contracts\FlightDynamicInterface');
        $whereIn = $flightDynamicService->searchFlightDynamic(request());
        $where = [];
        if (request()->start_at){
            $where[] = ['created_at','>=',substr(request()->start_at,0,10)];
        }

        $dynamics = $interface->get($where,'*',[],'id','desc','flight_id',$whereIn);
        if ($dynamics->isNotEmpty()) {

            foreach ($dynamics as $key=>$value) {

                $sumDelayRate = sprintf('%.2f',($value['dep_delay_inThirtyM'] + $value['dep_delay_thirtyMToOneH'] + $value['dep_delay_oneHToThreeH'] + $value['dep_delay_overThreeH']) / 100);
                $sumDelayRate = 30 * $sumDelayRate;


                $sumScheduleRate = sprintf('%.2f',($value['dep_delay_thirtyMToOneH'] + $value['dep_delay_oneHToThreeH'] + $value['dep_delay_overThreeH'] + ($value['scheduleChange_rate'] ? : 0))/100);
                $sumDelayRate = $sumDelayRate * $sumScheduleRate;
                $sumDelayRate = sprintf('%.2f',$sumDelayRate /30);
                if ( $sumDelayRate >= 0.6 ) {

                    $data[] = [
                        $value->created_at->format('Y-m-d'),
                        $value['flight_code'],
                        $value['dep_code'],
                        $value['arr_code'],
                        $value['dep_ontime_rate'],
                        $value['arr_ontime_rate'],
                        $value['dep_delay_inThirtyM'],
                        $value['dep_delay_thirtyMToOneH'],
                        $value['dep_delay_oneHToThreeH'],
                        $value['dep_delay_overThreeH'],
                        $value['arr_delay_inThirtyM'],
                        $value['arr_delay_thirtyMToOneH'],
                        $value['arr_delay_oneHToThreeH'],
                        $value['arr_delay_overThreeH'],
                        $value['dep_avgDelayTime'],
                        $value['arr_avgDelayTime'],
                        $value['cancel_rate'],
                        $value['sevenDay_ontime'],
                        $value['scheduleChange_rate'],
                        $value['lastChangeFlights'],
                        $sumDelayRate
                    ];
                }




            }

        }

        return collect($data);

    }
}
